package com.weblearning.tliasweb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.weblearning.tliasweb.mapper.EmpMapper;
import com.weblearning.tliasweb.pojo.Emp;
import com.weblearning.tliasweb.pojo.PageBean;
import com.weblearning.tliasweb.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, short gender, LocalDate begin, LocalDate end) {
        //设计分页
        PageHelper.startPage(page,pageSize);

        //执行查询
        List<Emp> empList=empMapper.list(name, gender,  begin, end);
        Page<Emp> p= (Page<Emp>) empList;

        //封装PageBean
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }
//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        long count =empMapper.count();
//        Integer start=(page-1)*pageSize;
//        List<Emp> empList = empMapper.page(start, pageSize);
//        PageBean p =new PageBean(count,empList);
//        return p;
//    }


    public  void delete(List<Integer> ids){
        empMapper.delete(ids);
    }

    @Override
    public void deleteByDeptId(Integer id) {
        empMapper.deleteByDeptId(id);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDate.now());
        emp.setUpdateTime(LocalDate.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp getById(Integer id) {
        Emp emp=empMapper.getById(id);
        return emp;
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDate.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.getByUsernameAndPassword(emp);
        return e;
    }


}
